package com.kele.order.service;

import com.kele.common.model.response.Result;
import com.kele.model.goods.Goods;
import com.kele.model.goods.dto.GoodsDTO;
import com.kele.model.order.OrderItem;
import com.kele.order.feign.GoodsFeignClient;
import com.kele.order.model.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class CartService {

    private static  final String CART_LIST = "cartList";
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    GoodsFeignClient goodsFeignClient;

    public Result addToCart(String userId,Long goodsId, Integer num){
        GoodsDTO goodsDTO = goodsFeignClient.selectById(goodsId);
        Goods goods = goodsDTO.getGoods();
        OrderItem orderItem = new OrderItem();
        orderItem.setGoodsId(goodsId);
        orderItem.setNum(num);
        orderItem.setPicPath(goods.getMainPic());
        //orderItem.setOrderId();
        orderItem.setPrice(goods.getPrice());
        orderItem.setTitle(goods.getCaption());
        orderItem.setTotalFee(goods.getPrice()
                .multiply(new BigDecimal(num)));

        Cart cart_user = (Cart)redisTemplate.opsForHash().get(CART_LIST, userId);
        if(cart_user==null){//第一次创建购物车
            cart_user = new Cart();
            if(orderItem.getNum()>0){
                cart_user.add(orderItem);
            }
            redisTemplate.opsForHash().put(CART_LIST,userId,cart_user);
        }else{
            Cart newCart_user = mergeCart(cart_user, orderItem);
            redisTemplate.opsForHash().put(CART_LIST,userId,newCart_user);
        }
        return Result.SUCCESS();
    }

    private Cart mergeCart(Cart oldCart,OrderItem orderItem){
        Cart newCart = new Cart();

        boolean isNotExistInOldCart = true;
        for (OrderItem oldOrderItem : oldCart) {
            if(oldOrderItem.getGoodsId().equals(orderItem.getGoodsId())) {//已经存在
                orderItem.setNum(oldOrderItem.getNum() + orderItem.getNum());
                orderItem.setTotalFee(orderItem.getPrice().multiply(
                        new BigDecimal(orderItem.getNum())
                ));
                if (orderItem.getNum() > 0) {
                    newCart.add(orderItem);
                }
                isNotExistInOldCart = false;
            }else{
                if(oldOrderItem.getNum()>0){
                    newCart.add(oldOrderItem);
                }
            }
        }
        if(isNotExistInOldCart){
            if(orderItem.getNum()>0){
                newCart.add(orderItem);
            }
        }

        return newCart;
    }

    public Cart getCart(String userid) {
        Cart cart_user = (Cart)
                redisTemplate.opsForHash().get(CART_LIST, userid);

        return cart_user;
    }

    public void resetCart(String userid,Cart newCart){
        redisTemplate.opsForHash().put(CART_LIST, userid,newCart);
    }
}
